package com.shiguo365.market.display.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shiguo365.market.R;
import com.shiguo365.market.presenter.contract.IConfirmPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/12.
 */
public class ConfirmAdapter extends RecyclerView.Adapter<ConfirmAdapter.ViewHolder>{

    List<Object> jsonObjectList = new ArrayList<Object>();
    IConfirmPresenter iConfirmPresenter;

    public ConfirmAdapter(IConfirmPresenter iConfirmPresenter,List<Object> jsonObjectList) {
        this.jsonObjectList = jsonObjectList;
        this.iConfirmPresenter =iConfirmPresenter;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_confirm,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.update(jsonObjectList.get(position));

    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return jsonObjectList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public  class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvPname;
        public TextView mTvSpname;
        public TextView mTvPrice;
        public TextView mTvPnum;
        public ImageView mImageView;

        Object object;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this, itemView);
            mTvPname = (TextView) view.findViewById(R.id.textViewOrderName);
            mTvSpname = (TextView) view.findViewById(R.id.textViewOrderSpecname);
            mTvPrice = (TextView) view.findViewById(R.id.textViewOrderPrice);
            mTvPnum = (TextView) view.findViewById(R.id.textViewOrderPnum);
            mImageView = (ImageView) view.findViewById(R.id.imageViewOrderImage);
        }

        public void update(Object object){
            this.object=object;
            JSONObject jo = (JSONObject) object;
            this.mTvPname.setText(jo.getString("name"));
            this.mTvSpname.setText(jo.getString("quantity"));
            this.mTvPrice.setText(jo.getString("price"));
            this.mTvPnum.setText("x"+jo.getIntValue("pnum"));

            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(jo.getString("main_pic"), mImageView);

        }
    }
}
